package com.titan.demo.camel.sample.数据转换.processor方式;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;

import com.titan.demo.camel.utils.RunCamel;

public class Test01 {
	public static void main(String[] args) {
        try {
        	RouteBuilder route = createRouteBuilder();
        	createRunCamel().run(route);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	
	private static RunCamel createRunCamel() {
		return new RunCamel() {
			protected void execute(CamelContext context,
					ProducerTemplate producer, ConsumerTemplate consumer)
					throws Exception {
				Map<String,String> map = new HashMap<String,String>();
				map.put("key1", "值1");
				map.put("key2", "value2");
				producer.sendBody("direct:in", map);
			}
		};
	}
	
	private static RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:in")
                .to("stream:out")
                .process(new Processor() {
                    public void process(Exchange e) {
                    	Map<String,String> body = e.getIn().getBody(Map.class);
                    	
                    	StringBuffer buffer = new StringBuffer(128);
                    	buffer.append("key1=").append(body.get("key1")).append(";");
                    	buffer.append("key2=").append(body.get("key2")).append(";");
                    	e.getOut().setBody(buffer);
                    }
                })
                .to("stream:out")
                ;
            }
        };
    }	
}
